package com.wxd.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.wxd.service.DevService;
import com.wxd.service.impl.DevServiceImpl;
import com.wxd.util.OutPut;

public class DevRunInfoAction extends ActionSupport {
	private DevService devService = new DevServiceImpl();

	// 获得设备运行时间信息
	public void getDevRunInfo() {
		HttpServletRequest request = ServletActionContext.getRequest();
		// 获得分页参数
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");

		// 获得过滤参数
		String devSpi = request.getParameter("devSpi");
		String ownUnit = request.getParameter("ownUnit");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");

		int startNum = 0;
		int limitNum = 50;
		JSONObject result = new JSONObject();

		Map statistics = devService.statistics(devSpi, ownUnit, startTime,
				endTime);
		int devCount = 0;
		int recordCount = 0;
		int totalRunTime = 0;
		int totalDistance = 0;
		if (statistics != null) {
			try {
				devCount = Integer.parseInt(statistics.get(1).toString());
				recordCount = (int) Double.parseDouble(statistics.get(2)
						.toString());
				totalRunTime = (int) Double.parseDouble(statistics.get(3)
						.toString());
				totalDistance = (int) Double.parseDouble(statistics.get(4)
						.toString());
			} catch (Exception e) {
			}
		}

		result.put("devCount", devCount);
		result.put("TOTALCOUNT", recordCount);
		result.put("totalRunTime", totalRunTime);
		result.put("totalDistance", totalDistance);
		if (recordCount != 0) {
			if (start != null) {
				try {
					startNum = Integer.parseInt(start);
					if (startNum >= recordCount) {
						startNum = 0;
					}
				} catch (Exception e) {
					System.out.println("数字转换这里");
				}
			}
			if (limit != null) {
				try {
					limitNum = Integer.parseInt(limit);
					if (limitNum == 0) {
						limitNum = 50;
					}
				} catch (Exception e) {
					System.out.println("数字转换这里");
				}
			}

			JSONArray devRunInfo = devService.getDevRunInfo(startNum, limitNum,
					devSpi, ownUnit, startTime, endTime);
			result.put("ROOT", devRunInfo);
		} else {
			result.put("ROOT", new JSONArray());
		}

		HttpServletResponse response = ServletActionContext.getResponse();

		OutPut.responseWrite(response, result.toString());
	}

	// 获得所有的规格列表
	public void getDevSpiList() {

		JSONArray devSpiArray = devService.getDevSpiList();


		HttpServletResponse response = ServletActionContext.getResponse();
		OutPut.responseWrite(response, devSpiArray.toString());
	}

	// 获得所有的单位列表
	public void getOwnUnitList() {

		JSONArray ownUnitArray = devService.getOwnUnitList();


		HttpServletResponse response = ServletActionContext.getResponse();
		response.addHeader("content-type","text/plain");
		OutPut.responseWrite(response, ownUnitArray.toString());
	}
	
	//获得所有操作员列表
	public void getOperatorList(){
		JSONObject result = new JSONObject();

		JSONArray operatorArray = devService.getOperatorList();

		result.put("root", operatorArray);

		HttpServletResponse response = ServletActionContext.getResponse();
		OutPut.responseWrite(response, result.toString());
	}
	
	//获得所有设备列表,用于combo {devCode,devName}
	public void getDevList(){
		JSONObject result = new JSONObject();

		JSONArray array = devService.getDevList();

		result.put("root", array);

		HttpServletResponse response = ServletActionContext.getResponse();
		OutPut.responseWrite(response, result.toString());
	}
	
	
	
	// 下载Excel
	public void downloadExcel() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String devSpi = request.getParameter("devSpi");
		String ownUnit = request.getParameter("ownUnit");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");

		HSSFWorkbook workbook = devService.getExcelInfo(devSpi, ownUnit,
				startTime, endTime);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setHeader("Content-Type", "application/force-download");
		response.setHeader("Content-Type", "application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=devRunInfo.xls");
		try {
			ServletOutputStream out = response.getOutputStream();
			workbook.write(out);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void devCount() {
		HttpServletRequest req = ServletActionContext.getRequest();
		String type = req.getParameter("type");
		if (type == null) {
			type = "1";
		}
		
		StringBuffer devCount = devService.devCount(type);
		
		// 添加bom
		byte[] utf8Bom = new byte[] { (byte) 0xef, (byte) 0xbb, (byte) 0xbf };
		String utf8BomStr = "";
		try {
			utf8BomStr = new String(utf8Bom, "UTF-8");// 定义BOM标记
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		devCount.insert(0, utf8BomStr);

		HttpServletResponse response = ServletActionContext.getResponse();
		OutPut.responseWrite(response, devCount.toString());
	}

	// ///////////////////////////////////页面跳转
	// 跳转到设备运行信息表页面
	public String xxzlPanelPage() {
		return SUCCESS;
	}

	// 跳转到设备设备数量图表页面
	public String firstPanelPage() {
		return SUCCESS;
	}

	// 跳转到设备基本信息页面
	public String jbxxPanelPage() {
		return SUCCESS;
	}
	
	// 跳转到设备维护信息页面
	public String whxxPanelPage() {
		return SUCCESS;
	}
	
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	// 跳转到折线图页面
	public String zhexianPanelPage() {
		HttpServletRequest request = ServletActionContext.getRequest();
		JSONArray ownUnitList = devService.getOwnUnitList();
		request.setAttribute("ownUnitStr", ownUnitList.toString());
		return SUCCESS;
	}

	// ///////////////////////////////////////////////设备基本信息

	public void getDevBaseInfo() {
		HttpServletRequest request = ServletActionContext.getRequest();

		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		
		// 获得过滤参数
		String devSpi = request.getParameter("devSpi");
		String ownUnit = request.getParameter("ownUnit");
		String operator = request.getParameter("operator");

		int totalRecord = devService.getDevBaseRecordCount(devSpi,ownUnit,operator);

		JSONObject result = new JSONObject();
		result.put("TOTALCOUNT", totalRecord);
		if (totalRecord != 0) {
			int startNum = 0;
			int limitNum = 50;
			if (start != null) {
				try {
					startNum = Integer.parseInt(start);
					if (startNum >= totalRecord) {
						startNum = 0;
					}
				} catch (Exception e) {
					System.out.println("数字转换这里");
				}
			}
			if (limit != null) {
				try {
					limitNum = Integer.parseInt(limit);
					if (limitNum == 0) {
						limitNum = 50;
					}
				} catch (Exception e) {
					System.out.println("数字转换这里");
				}
			}
			JSONArray geDevBaseInfo = devService.geDevBaseInfo(startNum,
					limitNum,devSpi,ownUnit,operator);
			result.put("ROOT", geDevBaseInfo);
		} else {
			result.put("ROOT", new JSONArray());
		}

		HttpServletResponse response = ServletActionContext.getResponse();
		OutPut.responseWrite(response, result.toString());
	}
	
	//下载设备基本信息excel
	public void downloadDevBaseInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String devSpi = request.getParameter("devSpi");
		String ownUnit = request.getParameter("ownUnit");
		String operator = request.getParameter("operator");

		HSSFWorkbook workbook = devService.getDevBaseExcelInfo(devSpi, ownUnit,operator);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setHeader("Content-Type", "application/force-download");
		response.setHeader("Content-Type", "application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=devBaseInfo.xls");
		try {
			ServletOutputStream out = response.getOutputStream();
			workbook.write(out);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	///////////////////设备维护信息
	public void getDevMaintainInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();

		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		
		
		
		// 获得过滤参数
		String devCode = request.getParameter("devCode");
		String devSpi = request.getParameter("devSpi");
		String ownUnit = request.getParameter("ownUnit");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");

		int totalRecord = devService.getDevMaintainRecordCount(devCode,devSpi,ownUnit,startTime,endTime);

		JSONObject result = new JSONObject();
		result.put("TOTALCOUNT", totalRecord);
		if (totalRecord != 0) {
			int startNum = 0;
			int limitNum = 50;
			if (start != null) {
				try {
					startNum = Integer.parseInt(start);
					if (startNum >= totalRecord) {
						startNum = 0;
					}
				} catch (Exception e) {
					System.out.println("数字转换这里");
				}
			}
			if (limit != null) {
				try {
					limitNum = Integer.parseInt(limit);
					if (limitNum == 0) {
						limitNum = 50;
					}
				} catch (Exception e) {
					System.out.println("数字转换这里");
				}
			}
			JSONArray geDevBaseInfo = devService.getDevMaintainInfo(startNum,
					limitNum,devCode,devSpi,ownUnit,startTime,endTime);
			result.put("ROOT", geDevBaseInfo);
		} else {
			result.put("ROOT", new JSONArray());
		}

		HttpServletResponse response = ServletActionContext.getResponse();
		OutPut.responseWrite(response, result.toString());
	}
	
	public void downloadDevWHInfo(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String devCode = request.getParameter("devCode");
		String devSpi = request.getParameter("devSpi");
		String ownUnit = request.getParameter("ownUnit");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");

		HSSFWorkbook workbook = devService.downloadDevWHInfo(devCode,devSpi, ownUnit,startTime,endTime);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setHeader("Content-Type", "application/force-download");
		response.setHeader("Content-Type", "application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=devMaintainInfo.xls");
		try {
			ServletOutputStream out = response.getOutputStream();
			workbook.write(out);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public void getDevMonthRunInfo(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String year = request.getParameter("year");
		int y ;
		if(year==null){
			y = Calendar.getInstance().get(Calendar.YEAR);
		}else{
			try {
				y = Integer.parseInt(year);
			} catch (Exception e) {
				y = Calendar.getInstance().get(Calendar.YEAR);
			}
		}
		
		JSONArray devMonthRunInfo = devService.getDevMonthRunInfo(y);
		HttpServletResponse response = ServletActionContext.getResponse();
		OutPut.responseWrite(response, devMonthRunInfo.toString());
	}
	
	
	
	public void getTopRunTimeInfo(){
		JSONObject result = new JSONObject();
		JSONArray array = devService.getTopRunTimeInfo();
		result.put("root", array);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		OutPut.responseWrite(response, result.toString());
	}
	
	public void getTopRunDistanceInfo(){
		JSONObject result = new JSONObject();
		JSONArray array = devService.getTopRunDistanceInfo();
		result.put("root", array);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		OutPut.responseWrite(response, result.toString());
	}
}
